import { requestOrigin, extend } from '@/services/request';
import { MyStatus, ProcessActions } from '@/pages/approval/constants';
import { parseConditionsValue } from '@/plugins/form/form-render/utils/detail-parser';
import { RUC_SERVICE, ACE_BPM, TS_STOCK } from '@/services/constant';

const request = extend({
  prefix: ACE_BPM
});

// 获取全部审批列表
export async function getMyProcessModel() {
  return request('/procRecord/myProcessModel', {
    method: 'POST',
    data: {}
  });
}
// 获取-提交表单前-审批流程
export interface IGetProcessTasksReq {
  innerProcDefId: string;
  forms: { formModelData: string; formValues: string };
}
export async function getProcessTasks(params: IGetProcessTasksReq) {
  return request('/procRecord/getProcessTasks', {
    method: 'POST',
    data: { ...params }
  });
}
// 发起流程
export interface IStartProcessReq {
  forms: { formModelData: string; formValues: string };
  taskCondition: any;
  innerProcDefId: string;
}
export async function startProcess(params: IStartProcessReq) {
  const formdata = params.forms.formValues;
  const values = JSON.parse(formdata);
  const taskCondition = parseConditionsValue(values);
  params.taskCondition = taskCondition;
  return request('/procRecord/startProcess', {
    method: 'POST',
    data: { ...params }
  });
}

// 待我审批/我已审批
export interface IMyTaskListReq {
  // applicant?:string,
  // title?:string,//
  keyword?: string; // 搜索条件
  operationType: MyStatus;
  pageNum?: number;
  pageSize: number;
  lastId?: string; // 流式加载传入最后一项id
}

export interface IMyTaskListApiReq {
  lastId: string;
  pageSize: number;
  operationType: number;
  pageNum?: number;
}

export async function getMyTaskList(params: IMyTaskListReq) {
  return request('/taskRecord/myTaskList', {
    method: 'POST',
    data: { ...params }
  });
}

// {"lastId":"","pageSize":10,"operationType":前端传的Table标识 1.待我审批 2.我已审批 3.抄送我的"pageNum":1}
// 待我审批的记录 (待我审批/我的审批) 简版，只有基本字段
export function getMyTaskListApi(params: IMyTaskListReq) {
  return request('/apiTaskRecord/myTaskList', {
    method: 'POST',
    data: { ...params }
  });
}

// 快捷审批处理
export function setTaskAction(data: any) {
  return request('/taskRecord/fastTaskAction', {
    method: 'POST',
    data
  });
}

// 批量快捷审批处理
export function setBatchTaskAction(data: any) {
  return request('/taskRecord/batchTaskAction', {
    method: 'POST',
    data
  });
}

export async function getMyProcessList(params: any) {
  return request('/api/procRecord/myProcessList', {
    method: 'POST',
    data: { ...params }
  });
}

export interface IReqCopyList {
  keyword?: string;
  pageNum: number;
  pageSize: number;
}
// 获取抄送给我的列表
export async function getCopyToMeList(params: IReqCopyList) {
  return request('/taskRecord/csToMyTaskList', {
    method: 'POST',
    data: { ...params }
  });
}
export interface IReqReadCopy {
  taskRecordId: string;
}
// 抄送消息已读取
export async function readCopyToMe(params: IReqReadCopy) {
  return request('/taskRecord/read', {
    method: 'GET',
    params: { ...params }
  });
}

export interface IParamsForm {
  formValues: string;
  formModelData: string;
}
// 审批任务
export interface IApprovalActionReq {
  forms?: IParamsForm;
  taskCondition?: any;
  action: string;
  handlerComment: string; // 处理意见
  taskRecordId: string | undefined;
}
export async function doTask(params: IApprovalActionReq) {
  if (params.forms) {
    const formdata = params.forms.formValues;
    const values = JSON.parse(formdata);
    const taskCondition = parseConditionsValue(values);
    params.taskCondition = taskCondition;
  }
  return request('/taskRecord/taskAction', {
    method: 'POST',
    data: { ...params }
  });
}

// 获取表单
export interface IDetailFormsReq {
  innerProcDefId: string;
}
export async function detailForm(params: IDetailFormsReq) {
  return request(
    `/flowableForm/queryByInnerProcDefId?innerProcDefId=${params.innerProcDefId}`,
    {
      method: 'GET',
      data: { ...params }
    }
  );
}

// 获取表单
export async function getMyTaskCount() {
  return request('/taskRecord/myTaskConut', {
    method: 'POST'
  });
}

// 审批任务
export interface IMyApplyActionReq {
  action: ProcessActions;
  procRecordId: string;
  taskCondition: any;
  forms?: {
    formModelData: string;
    formValues: string;
  };
}
export async function changeMyApplyProcess(params: IMyApplyActionReq) {
  if (params.forms) {
    const formdata = params.forms.formValues;
    const values = JSON.parse(formdata);
    const taskCondition = parseConditionsValue(values);
    params.taskCondition = taskCondition;
  }
  return request('/taskRecord/recallProcess', {
    method: 'POST',
    data: { ...params }
  });
}

// 审批任务
export interface IFormValue {
  procRecordId: string;
}
// export async function getFormValue(params: IFormValue) {
//   return request(`/taskRecord/formValue?procRecordId=${params}`, {
//     method: 'POST'
//   });
// }
export async function getFormValue(params: string) {
  return request(`/taskRecord/formValue?procRecordId=${params}`, {
    method: 'POST'
  });
}

// 催办审批
export interface IUrgingProcessReq {
  procNo: string;
  procName: string;
  innerProcDefId: string;
  procInstId: string;
  taskDefKey: string;
}
export async function urgingProcess(params: IUrgingProcessReq) {
  return request('/taskRecord/urgingProcess', {
    method: 'POST',
    data: { ...params }
  });
}

// 获取审批任务处理人
export interface ITaskHandlerReq {
  procNo: string;
  defKey: string;
}
export async function getTaskHandler(params: ITaskHandlerReq) {
  return request(
    `/taskRecord/getTaskHandlerByProcNoAndDefKey?procNo=${params.procNo}&defKey=${params.defKey}`,
    {
      method: 'POST'
    }
  );
}

// 审批详情-我发起的
export interface IProcDetailReq {
  procRecordId: string;
}
export async function getProcDetail(params: IProcDetailReq) {
  return request(`/procRecord/procDetail?procRecordId=${params.procRecordId}`, {
    method: 'GET',
    data: { ...params }
  });
}
// 审批任务的详情-待审批/已抄送/已审批
export interface ITaskDetailReq {
  taskRecordId: string;
}
export async function getTaskDetail(params: ITaskDetailReq) {
  return request(
    `/apiTaskRecord/taskDetail?taskRecordId=${params.taskRecordId}`,
    {
      method: 'GET',
      data: { ...params }
    }
  );
}
// 获取-审批流程
export interface IHistoryTaskListReq {
  innerProcDefId: string;
  procInstId: string;
}
export async function getHistoryTaskList(params: IHistoryTaskListReq) {
  return request(
    `/taskRecord/historyTaskList?innerProcDefId=${params.innerProcDefId}&procInstId=${params.procInstId}`,
    {
      method: 'POST'
    }
  );
}

export async function getNoticeList() {
  return request('/notice/v1/notice/list', {
    method: 'POST'
  });
}
